var fs = require('fs')
var path = require('path')
var ip = require('ip');
var webpack = require('webpack')
var webpackDevMiddleware = require('webpack-dev-middleware')
var webpackHotMiddleware = require('webpack-hot-middleware')
var proxy = require('http-proxy-middleware')
var history = require('connect-history-api-fallback');
var express = require('express')
var app = express()
var config = require('./webpack.dev.conf')
var compiler = webpack(config)
var host = ip.address();
var port = process.env.PORT || 3002;//开发环境端口;
app.use(history());

config.entry.bundle.unshift ('webpack-hot-middleware/client?quiet=true')
config.plugins.unshift(new webpack.HotModuleReplacementPlugin());

app.use(webpackDevMiddleware(compiler,{
	publicPath: host + ':'+port+'/',
  quite:true,
	noInfo:false,
	stats:{
    colors:true,
		chunks: false
  }
}))

app.use(webpackHotMiddleware(compiler))
//app.use('/assets/',express.static(path.join(__dirname,'static')))
compiler.plugin('compilation', function (compilation) {
  compilation.plugin('html-webpack-plugin-after-emit', function (data, cb) {
    hotMiddleware.publish({ action: 'reload' })
    cb()
  })
})

//接口代理，解决跨域问题
app.use('/api',proxy({
  target:'http://127.0.0.1:3008',
  pathRewrite:{
    '^/api' : '/service'
  },
  changeOrigin: false,
}));

module.exports = app.listen(port, function (err) {
  if (err) {
    console.log(err)
    return
  }
  var uri = host + ':' + port
  console.log('Listening at ' + uri + '\n');

})
